$(function () {
    getGradeList();
    allPictureRecordingTable();
    var imgPath = "";
    var videoPath = "";
})

function getGradeList() {
    $.DemoPost("/grade/getGradePageByUserInfo?token="+sessionStorage.getItem("token"),
        {"pageNum":1,"pageSize":100},
        function (data) {
            if(data.success)
            {
                $.each(data.obj.list,function (i,d) {
                    $("#gradeSelect").append(" <option value='"+d.id+"'>"+d.gradeName+"</option>");
                });
            }
            else{
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("初始化数据失败");
        });
}

function gradeChang() {
    var gradeId = $("#gradeSelect").val();
    if(gradeId=='')
    {
        $("#classSelect").html( "<option value=''>请选择</option>");
        return;
    }
    $.DemoGet("/classInfo/getClassListByGradeId/"+gradeId,function (data) {
        if(data.success)
        {
            $("#classSelect").html( "<option value=''>请选择</option>");
            $.each(data.obj,function (i,d) {
                $("#classSelect").append(" <option value='"+d.id+"'>"+d.className+"</option>");
            });
        }
        else
        {
            layer.msg(data.msg);
        }
    },function (e) {
        layer.msg("操作失败");
    });
}

function query(params) {
    var gradeId = $("#gradeSelect").val();
    var classId = $("#classSelect").val();
    var title = $("#queryVal").val();
    var param = {
        title:title,
        gradeId:gradeId,
        classId:classId,
        pageNum : params.offset / params.limit + 1,
        pageSize : params.limit
    };
    return param;
}
/*获取列表数据*/
function allPictureRecordingTable() {
    $("#yb_list").bootstrapTable({
        url:$.ip+"/picturerecording/getPictureRecordingByUserInfo?token="+sessionStorage.getItem("token"),
        method:'post',
        queryParams:query,
        sidePagination: "server",
        pagination: true,
        striped:true,
        pageNumber:1,
        pageSize: 10,
        pageList: [10, 25, 50, 100],
        dataType:"json",
        contentType:"application/json",
        responseHandler:function (res) {
            return {
                total:res.obj.total,
                rows :res.obj.list
            }
        },
        columns: [
            {
                title: '序号',
                field: 'id',
                align: 'center',
                valign: 'middle',
                formatter:function (value,row,index){
                    return index+1;
                }
            },
            {
                title: '标题',
                align: 'center',
                field: 'title',
                valign: 'middle',

            } ,
            {
                title: '副标题',
                align: 'center',
                field: 'subhead',
                valign: 'middle',

            }  ,
            {
                title: '上传时间',
                align: 'center',
                field: 'createdAtStr',
                valign: 'middle',
            }  ,
            {
                title: '操作',
                align: 'center',
                field: 'id',
                width:240,
                valign: 'middle',
                formatter:function (id,row) {
                    return '<a onclick="remove(this)" data-id="'+id+'" class="btn btn-danger" href="javascript:;">删除</a>&nbsp<a onclick="info(this)" data-id="'+id+'" class="btn btn-primary " href="javascript:;">查看</a> ';
                }
            },
        ]
    });
}


function remove(obj){
    var id = $(obj).data("id");
    tan("是否执行当前操作？",function () {
       $.DemoGet("/picturerecording/delete/"+id,
           function (data) {
                if(data.success)
                {
                    $("#yb_list").bootstrapTable('refresh');
                }
                else
               {
                   layer.msg(data.msg);
               }
           },function (e) {
               layer.msg();
           });

    });

}
function add() {
     imgPath = "";
     videoPath = "";
    $("#title_input").val("");
    $("#subhead_input").val("");
    $("#pictureDesc_input").val("");
    popdiv("add-info","添加",["50%","80%"],function (index,layero) {
        var title = $("#title_input").val();
        var subhead = $("#subhead_input").val();
        var pictureDesc = $("#pictureDesc_input").val();
        var pageImgUrl = imgPath;
        var videoUrl = videoPath;
        if(title=='')
        {
            layer.msg("标题不能为空");
        }
        else if(subhead=='')
        {
            layer.msg("副标题不能为空");
        }else if(pictureDesc=='')
        {
            layer.msg("介绍不能为空");
        }
        else if(pageImgUrl=='')
        {
            layer.msg("请上传封面图片");
        }else if(videoUrl=='')
        {
            layer.msg("请上传视频");
        }
        else
        {
            $.DemoPost("/picturerecording/add?token="+sessionStorage.getItem("token"),
                {"title":title,"subhead":subhead,"pictureDesc":pictureDesc,"pageImgUrl":pageImgUrl,"videoUrl":videoUrl},
                function (data) {
                    if(data.success)
                    {
                        layer.close(index);
                        $("#yb_list").bootstrapTable('refresh');
                    }
                    else {
                        layer.msg(data.msg);
                    }
                },function (e) {
                    layer.msg("添加演播数据失败");
                });
        }


    })
}
function update() {
    popdiv("add-info","修改",["50%","50%"],function (index,layero) {
        tan("是否执行当前操作？",function () {
            layer.alert("操作成功");
        });
        layer.close(index);
    })
}
function info(obj){
    var id = $(obj).data("id");
    $.DemoGet("/picturerecording/info/"+id,
        function (data) {
            if(data.success)
            {
                var videoHtml =
                    '<video id="video" class=\'video-js  vjs-default-skin\' controls  preload=\'auto\' poster="'+data.obj.pageImgUrl+'" data-setup=\'{}\'>\n' +
                    '                <source src="'+data.obj.videoUrl+'" type="video/mp4">\n' +
                    '            </video>';
                $("#videoDiv").html(videoHtml);
                $("#subhead").text(data.obj.subhead);
                $("#pictureDesc").text(data.obj.pictureDesc);
                lookInfo("class-info","查看",["70%","70%"]);
            }
            else {
                layer.msg(data.msg);
            }
        },function (e) {
            layer.msg("获取详情失败");
        });

}
/*设置flash路径，Video.js会在不支持html5的浏览中使用flash播放视频文件  videojs.options.flash.swf = "video-js.swf";*/
var myPlayer = videojs('video');


$("#btnQuery").click(function () {
    $("#yb_list").bootstrapTable('refresh');
});
//封面预览
function upCoverImg(obj) {
    if (obj.files && obj.files[0]) {
        var formData = new FormData();
        formData.append('file', $('#file')[0].files[0]);
        $.ajax({
            url: 'http://139.129.213.125:6060/sports/api/picture',
            type: 'POST',
            cache: false,
            data:formData,
            processData: false,
            contentType: false
        }).done(function (res) {
            console.log(res);
            imgPath = res[0];
           $("#homePageImg").prop("src",imgPath);
        }).fail(function (res) {
        });
    }else{
        alert("浏览器不兼容")
    }
}


function videoChange(obj) {
    if (obj.files && obj.files[0]) {
        var formData = new FormData();
        formData.append('file', $('#filVideo')[0].files[0]);
        $.ajax({
            url: 'http://139.129.213.125:6060/sports/api/video',
            type: 'POST',
            cache: false,
            data:formData,
            processData: false,
            contentType: false
        }).done(function (res) {
            console.log(res);
            videoPath = res;
            var html = ' <video  class=\'video-js   vjs-default-skin\' width="300" height="200" controls  preload=\'auto\'  data-setup=\'{}\'>\n' +
                '                                <source src="'+videoPath+'" type="video/mp4">\n' +
                '                            </video>';

            $("#videoContent").html(html);
        }).fail(function (res) {
        });
    }else{
        alert("浏览器不兼容")
    }
}